import pandas as pd
from tqdm import tqdm
import utils


def colorfy(color_df, sht, target_columns, mapping=None):
    all_columns = color_df.columns.to_list()
    for column in tqdm(target_columns):
        col_length = len(color_df[all_columns[column]].dropna())
        sht_col = sht.range(
            utils.get_col(column)
            + str(2)
            + ":"
            + utils.get_col(column)
            + str(1 + col_length)
        )
        for sht_cell in sht_col:
            if mapping:
                sht_cell.color = [
                    int(item)
                    for item in mapping[sht_cell.value].split("-")
                ]
            else:
                sht_cell.color = [
                    int(item) for item in sht_cell.value.split("-")
                ]


def main():
    file_name = "ColorDataSource.xlsx"
    color_df = pd.read_excel(file_name)
    color_df = pd.read_excel(file_name)
    color_df_cols = color_df.loc[0].apply(
        lambda item: item in color_df.color_hlc.to_list()
    )
    paint_cols_idx = [
        i
        for i, item in enumerate(color_df_cols)
        if item and color_df_cols.index[i] != "color_hlc"
    ]
    hlc_rgb = dict(zip(color_df.color_hlc, color_df.color_rgb))
    with utils.open_sht(file_name) as sht:
        colorfy(
            color_df,
            sht,
            paint_cols_idx,
            hlc_rgb,
        )


if __name__ == "__main__":
    main()